<?php
/**
 * Created by PhpStorm.
 * User: y3731
 * Date: 2018/12/13
 * Time: 13:44
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\admin\model\Devices as DevicesModel;
use app\common\model\Config;
use think\Db;

class Devices extends Api
{
    /**
     * @var null
     * 模型对象
     */
    protected $model = null;
    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    //状态码
    protected  $code;
    public function _initialize()
    {
        parent::_initialize();

        $this->model = new DevicesModel();
        $this->code  = config('code');
    }
    /**
     * 景区名称
     */
    public function scenicName()
    {
        $data = $this->request->param();
        if (!$this->request->isPost()){
            $this->error('请求方式不正确!',[], $this->code['error']);
        }

        if (!$data['devices_id']){
            $this->error('参数不能为空!',[],$this->code['error']);
        }

        $info = $this->model->where('devices_id',$data['devices_id'])->find();
        if (!$info){
            $this->error('无此设备！',[],$this->code['error']);
        }
        $data_s = array(
            'name'=>'红叶谷',
        );
        $this->success('ok',$data_s,$this->code['success'],'json');

    }

    /**
     * 设备登录
     */
    public function devicesLogin()
    {
       $data = $this->request->param();
       //halt($data);
       if (!$this->request->isPost()){
           $this->error('请求方式不正确!',[],$this->code['error']);
       }

       if (!$data['password'] && !$data['devices_id']){
           $this->error('参数不能为空',[],$this->code['error']);
       }

       $info = $this->model->where('devices_id',$data['devices_id'])->find();

       if ($info){

            //验证登录密码
           if ($info['password'] == $data['password']){
                   //状态
               if ($info['state']){
                   //绑定的景区
                  // $data_s = Db::name('Category')->where('id',$info['store_id'])->find();

                  // $data_s['token']=$this->set_token($info['devices_id']);

                   $data_s = array(
                        'id'=>1,
                       'name'=>'红叶谷',
                       'token'=>$this->set_token($info['devices_id'])
                   );

                   $this->success('ok',$data_s,$this->code['success']);

               }else{
                   $this->error('此设备已锁定',[],$this->code['error']);
               }
           }else{
               $this->error('密码错误!',[],$this->code['error']);
           }

       }else{
           $this->error('无此设备',[],$this->code['error']);
       }

    }


    /**
     * @param $id
     * @return string
     * @throws \think\exception\DbException
     * token生产
     */
    protected function set_token($devices_id){
        $options = [
          'type'=>'File',
            // 缓存前缀
            'prefix'      => Config::get('token.key'),
            // 缓存有效期 0表示永久缓存
            'expire'   => 0,//缓存7天
        ];
        $token = md5($devices_id.Config::get('token.hashalgo'));
        $handler = \app\common\library\Token::init($options);

        $handler->set($token,$devices_id);

        return $token;
    }

}